package com.VolunteerPlatform.Controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by DoveXL on 2018/5/30.
 */
@Controller
@RequestMapping(value = "/User/**")
public class LoginController {

    @RequestMapping(value="register",method = RequestMethod.GET)
    public String User_Register()
    {
        return "/register";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String  login(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout) {
        if (error != null) {
            return "/login";
        }
        if (logout != null) {
            return "/Logout";
        }
        return "/login";
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpServletRequest request, HttpServletResponse response)
    {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/User/login?logout";
    }

}
